import nltk
import random
import makebase

class pos:
    
    def __init__(self):
        self.tribase=makebase.tribase();
    def updateDB(self, sent, dir):
        updates=0;
        tagset=dict();
        trigs=dict();
        a=nltk.tokenize.word_tokenize(sent);
        a=nltk.pos_tag(a);
        tags=list(tag for word, tag in a);
        bads='IN', 'CC','TO', 'DT';
        for i in range(len(tags))[0:len(tags)-4]:
            k=tags[i],tags[i+1],tags[i+2],tags[i+3],tags[i+4];
            if not (k[4] in bads):
                    self.tribase.updateTags(k, dir);
                    updates+=1;
        tr=list((word, tag) for word, tag in a);
        for i in range(len(tr))[0:len(tr)-2]:
            k=tr[i],tr[i+1],tr[i+2];
            self.tribase.updateTrigs(k, dir);
            updates+=1;
        return updates;
    def maintenance(self):
        self.tribase.maintenance();
    def generateSent(self):
        outp='';
        outp=self.trigwords();
        return(outp);
    def trigwords(self):
        outp='';
        position=0;
        #Get first tagsets and split to sum(probability) and tuples
        res=self.tribase.getTags();
        tagprobs=res[0];
        tags=res[1];
        words=0;
        #at most 20 word long outputs
        while words<20:
            #If the first tagset failed
            if(len(tags)==0):
                res=self.tribase.getTags();
                tagprobs=res[0];
                tags=res[1];
                outp='';
                position=0;
            r=random.random();
            tagPointer=0;
            #Go through tagsets and pick one at random
            runner=0;
            for i in range(len(tags)):
                runner+=float(tags[i][0])/tagprobs;
                if r<=runner:
                    tagPointer=i;
                    break
                tagPointer=i;
                i+=1;
            #Take the tags to a new variable leave out the prob
            tagPart=tags[tagPointer][1:4];
            #Featch the trigram that fits the tagpart
            res=self.tribase.getTrigram(tagPart);
            triprobs=res[0][0];
            thing=res[1];
            if len(thing)>0:
                r=random.random();
                pointer=0;
                runner=0;
                #Pick one trigram at random
                for i in range(len(thing)):
                    runner+=float(thing[i][0])/triprobs;
                    if r<runner:
                        pointer=i;    
                        break;
                    pointer=i;
                    i+=1;
                #If the conditions are met append the whole trigram to the output and return
                if position>5 and r<.33:
                    outp+=str(thing[pointer][1])+' '+str(thing[pointer][2])+' '+str(thing[pointer][3])+'.';
                    position+=3;
                    break;
                #Otherwise put two first words in output and continue
                else:
                    outp+=str(thing[pointer][1])+' '+str(thing[pointer][2])+' ';
                    position+=2;
                    res=self.tribase.getTags(tags[tagPointer][1:6]);
                    tagprobs=res[0];
                    tags=res[1];
            else:
                res=self.tribase.getTags();
                tagprobs=res[0];
                tags=res[1];
                outp='';
                position=0;
            words=position;
        if(len(outp)>0):
            return outp;
   